package com.caiden_micheal.base.utils;

import com.caiden_micheal.base.properties.AESProperties;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.Random;

@Component
public class WorkNoGenerateUtil {
    @Autowired
    private static AESProperties aesProperties;

    @Resource
    public void setAESUtil(AESProperties aesProperties) {
        WorkNoGenerateUtil.aesProperties = aesProperties;
    }

    public static String generateWorkNo(Integer length) {
        StringBuffer stringBuffer = new StringBuffer();
        Random random = new Random();
        for (int i = 0; i < length; i++) {
            stringBuffer.append(random.nextInt(9));
        }
        int middleIndex = length / 2;
        for (int i = 0; i < middleIndex; i++) {
            stringBuffer.setCharAt(random.nextInt(length - 1), (char) (random.nextInt(9) + 97));
        }
        return aesProperties.getPrefixWorkno() + stringBuffer;
    }
}
